package FanShe1;

import java.lang.reflect.Field;

public class TestCar {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        String classAllPath = "FanShe1.Car";
        //获取反射类对象 cls
        Class<?> cls = Class.forName(classAllPath);
        //查看反射类对象 显示反射类对象是那个那类的反射类
        System.out.println("cls = " + cls);
        //输出运行类型
        System.out.println("cls.getClass() = " + cls.getClass());
        System.out.println("cls.getPackage().getName() = " + cls.getPackage().getName());

        System.out.println("cls.getName() = " + cls.getName());
        //通过反射实例化类的一个对象
        Car car = (Car) cls.newInstance();//new Car()
        Car car1 = new Car();
        System.out.println("car = " + car);
        System.out.println("car1 = " + car1);

        Field brand = cls.getField("brand");
        //参数传递的反射实例化的对象
        System.out.println("brand.get(car) = " + brand.get(car));

        brand.set(car,"奔驰");

        System.out.println("brand.get(car) = " + brand.get(car));

        System.out.println("所有的属性字段");
        Field[] fields = cls.getFields();
        for(Field f:fields)
        {
            System.out.println("f = " + f);
            System.out.println("f.getName() = " + f.getName());
        }


    }
}
